package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;

@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {

    @Reference
    OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile) throws IOException {
        log.debug("excelFile:"+excelFile);
        try {
            List<String[]> strings = POIUtils.readExcel(excelFile);
            List<OrderSetting> orderSettings = new ArrayList<>();
            for (String[] string : strings) {
                OrderSetting orderSetting = new OrderSetting();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                Date date = sdf.parse(string[0]);
                Integer integer = Integer.parseInt(string[1]);
                orderSetting.setOrderDate(date);
                orderSetting.setNumber(integer);
                orderSettings.add(orderSetting);
            }
            log.debug("excel表中数据："+orderSettings.toString());
            orderSettingService.addOrderSettingList(orderSettings);
        }
        catch (BusinessRuntimeException e) {
            e.printStackTrace();
            throw e;
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new BusinessRuntimeException(MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }


    @RequestMapping("/findByMonth")
    public Result findByMonth(String date) throws ParseException {
        log.debug("OrderSettingController: findByDate: " + date);
        List<OrderSetting> orderSettingList = orderSettingService.findByMonth(date);
        // 需要把数据转换为前端想要的数据
        List<Map<String,Object>> mapList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            //每一个orderSetting 对应一个Map集合
            Map<String,Object> map = new HashMap<>();
            //
            Date orderDate = orderSetting.getOrderDate();
            //把日期类型 获取里面的天
            SimpleDateFormat sdf = new SimpleDateFormat("dd");
            String orderDateStr = sdf.format(orderDate);
            map.put("date", orderDateStr);
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            //把map添加到集合中
            mapList.add(map);
        }
        log.debug(orderSettingList.toString());
        return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS , mapList);
    }
}
